<?php
/**
 * Created by Point.php.
 * User: wy
 * Date: 2019/7/12
 * Time: 13:00
 */

namespace app\admin\controller;


use my\FTP;
use my\Security;
use my\Tools;
use think\Db;
use think\Session;

class Point extends Base
{
    protected $commodity_type = ['虚拟商品','实物商品'];
    protected $is_grant = ['待充值','已充值'];
    protected $is_use = ['已下架','已上架'];

    public function index(){
        $page = input('get.page');
        $limit = input('get.limit');
        $map = [];
        if ($page && $limit) {
            if(input('get.commodity_type')!=''){
                $map['a.commodity_type'] =  input('get.commodity_type');
            }
            if(input('get.commodity_classification')!=''){
                $map['a.commodity_classification'] =  input('get.commodity_classification');
            }
            if(input('get.commodity_name')){
                $commodity_name = input('get.commodity_name');
                $map['a.commodity_name'] =  ['like',"%$commodity_name%"];
            }
            //活动开始时间
            if (input('get.valid_start_time')) {
                $map['a.valid_start_time'] = ['>=',input('get.valid_start_time')];
            }
            //活动结束时间
            if (input('get.valid_end_time')) {
                $map['a.valid_end_time'] = ['<',input('get.valid_end_time')];
            }
            if(input('get.is_use')!=''){
                $map['a.is_use'] =  input('get.is_use');
            }

            $start = ($page - 1) * $limit;
            $count = Db::name('commodity_classify a')
                ->where($map)->count();
            $list = Db::name('commodity_classify a')
                ->field('a.*,b.classification_name,c.user_name admin_name')
                ->join('xm_commodity_classification b','a.commodity_classification=b.classification_id','left')
                ->join('xm_admin c','a.admin_id=c.admin_id','left')
                ->where($map)->limit($start, $limit)->order('a.sort desc,a.commodity_id desc')->select();
            $commodity_type = $this->commodity_type;
            foreach ($list as $k=>$v){
                $list[$k]['commodity_type'] = $commodity_type[$v['commodity_type']];
                $list[$k]['is_use_str'] = $this->is_use[$v['is_use']];
            }
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list
            ]);
        }
        $cate = Db::name('commodity_classification')->column('classification_name','classification_id');
        $this->assign('cate',$cate);
        return $this->fetch();
    }

    public function add(){
        if(input('post.')){
            $data = input('post.');
            $data['add_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = date('Y-m-d H:i:s');
            $data['admin_id'] = session('admin_id');
            Db::name('commodity_classify')->insertGetId($data);
            return "<script type='application/javascript'>parent.window.location.reload();</script>";
        }
        $cate = Db::name('commodity_classification')->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }

    //修改
    public function edit(){
        if(input('post.')){
            if(input('post.commodity_id')){
                $data = input('post.');
                $data['update_time'] = date('Y-m-d H:i:s');
                $data['admin_id'] = session('admin_id');
                $result = Db::name('commodity_classify')->update($data);
                if($result!==false){
                    return "<script type='application/javascript'>alert('修改成功');parent.window.location.reload();</script>";
                    //$this->success('修改成功', '/admin/CarBrand/Index', '', 1);
                }
            }
        }elseif(input('get.commodity_id')){
            $commodity_id = input('get.commodity_id');
            $data = Db::name('commodity_classify')
                ->where('commodity_id',$commodity_id)->find();
            $this->assign('data',$data);
            $cate = Db::name('commodity_classification')->select();
            $this->assign('cate',$cate);
            return $this->fetch();
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    public function del(){
        if(input('get.commodity_id')){
            $commodity_id = input('get.commodity_id');
            $data = Db::name('commodity_classify')->where('commodity_id',$commodity_id)->delete();
            if($data){
                exit(json_encode(['status'=>1,'msg'=>'删除成功']));
            }else{
                exit(json_encode(['status'=>0,'msg'=>'删除失败']));
            }
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    public function indexCate(){
        $page = input('get.page');
        $limit = input('get.limit');
        $map = [];
        if ($page && $limit) {
            if(input('get.classification_name')){
                $classification_name = input('get.classification_name');
                $map['classification_name'] =  ['like',"%$classification_name%"];
            }
            $start = ($page - 1) * $limit;
            $count = Db::name('commodity_classification')
                ->where($map)->count();
            $list = Db::name('commodity_classification')
                ->where($map)->limit($start, $limit)->order('sort desc,classification_id asc')->select();
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list
            ]);
        }
        return $this->fetch();
    }

    public function addCate(){
        if(input('post.')){
            $data = input('post.');
            $map['classification_name'] = $data['classification_name'];
            $info = Db::name('commodity_classification')->where($map)->find();
            if(!empty($info)){
                $this->error('对不起该名称已存在', '', '', 1);
            }
            $add['classification_name'] = input('post.classification_name');
            $add['sort'] = input('post.sort');
            $add['add_time'] = date('Y-m-d H:i:s');
            $add['update_time'] = date('Y-m-d H:i:s');
            $add['admin_id'] = session('admin_id');
            Db::name('commodity_classification')->insertGetId($add);
            return "<script type='application/javascript'>parent.window.location.reload();</script>";
        }
        return $this->fetch();
    }

    public function editCate(){
        if(input('post.')){
            if(input('post.classification_id')){
                $data = input('post.');
                $map['classification_name'] = $data['classification_name'];
                $map['classification_id'] = ['<>',$data['classification_id']];
                $info = Db::name('commodity_classification')->where($map)->find();
                if(!empty($info)){
                    $this->error('对不起该名称已存在', '', '', 1);
                }
                $update['classification_id'] = input('post.classification_id');
                $update['classification_name'] = input('post.classification_name');
                $update['sort'] = input('post.sort');
                $update['update_time'] = date('Y-m-d H:i:s');
                $update['admin_id'] = session('admin_id');
                $result = Db::name('commodity_classification')->update($update);
                if($result!==false){
                    return "<script type='application/javascript'>alert('修改成功');parent.window.location.reload();</script>";
                    //$this->success('修改成功', '/admin/CarBrand/Index', '', 1);
                }
            }
        }elseif(input('get.classification_id')){
            $classification_id = input('get.classification_id');
            $data = Db::name('commodity_classification')
                ->where('classification_id',$classification_id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    public function delCate(){
        if(input('get.classification_id')){
            $classification_id = input('get.classification_id');
            $data = Db::name('commodity_classification')->where('classification_id',$classification_id)->delete();
            if($data){
                exit(json_encode(['status'=>1,'msg'=>'删除成功']));
            }else{
                exit(json_encode(['status'=>0,'msg'=>'删除失败']));
            }
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    public function upload(){
        $ftp = new FTP('','','','','WEB');
        $thumb = Tools::thumb($_FILES);//存放本地,返回也是$_FILES
        $_FILES = array_merge($_FILES,$thumb);
        $res_ftp = $ftp->url_ftpUploads($_FILES,'point');
        return $res_ftp;
    }

    public function indexChange(){
        $page = input('get.page');
        $limit = input('get.limit');
        $map = [];
        if ($page && $limit) {
            if(input('get.classification_id')!=''){
                $map['a.classification_id'] =  input('get.classification_id');
            }
            if(input('get.commodity_name')){
                $commodity_name = input('get.commodity_name');
                $map['a.commodity_name'] =  ['like',"%$commodity_name%"];
            }
            if(input('get.is_grant')!=''){
                $map['a.is_grant'] =  input('get.is_grant');
            }
            if (input('get.start_time') && input('get.end_time')) {
                $start_time = input('get.start_time');
                $end_time = input('get.end_time');
                $map['a.add_time'] = ['between', [$start_time, $end_time]];
            }
            if(input('get.user_phone')!=''){
                $map['b.user_phone'] =  Security::encrypt(input('get.user_phone'));
            }
            $start = ($page - 1) * $limit;
            $count = Db::name('commodity_conversion_record as a')
                ->join('xm_user b','a.uid=b.uid','left')
                ->where($map)->count();
            $list = Db::name('commodity_conversion_record as a')
                ->field('a.*,b.user_phone,c.user_name admin_name')
                ->join('xm_user b','a.uid=b.uid','left')
                ->join('xm_admin c','a.operator=c.admin_id','left')
                ->where($map)->limit($start, $limit)->order('a.id desc')->select();
            $is_grant = $this->is_grant;
            foreach ($list as $k=>$v){
                $list[$k]['user_phone'] = Security::decrypt($v['user_phone']);
                $list[$k]['is_grant'] = $is_grant[$v['is_grant']];
            }
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list
            ]);
        }
        $cate = Db::name('commodity_classification')->column('classification_name','classification_id');
        $this->assign('cate',$cate);
        return $this->fetch();
    }

    //充值审核
    public function rechargeChange(){
        if(input('get.id')){
            $id = input('get.id');
            $update['operator'] = session('admin_id');
            $update['update_time'] = date('Y-m-d H:i:s');
            $update['is_grant'] = 1;
            $data = Db::name('commodity_conversion_record')->where('id',$id)->update($update);
            if($data!==false){
                exit(json_encode(['status'=>1,'msg'=>'充值成功']));
            }else{
                exit(json_encode(['status'=>0,'msg'=>'充值失败']));
            }
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

    //上架和下架
    public function is_use(){
        if(input('get.commodity_id')){
            $commodity_id = input('get.commodity_id');
            $is_use = input('get.is_use');
            $update['admin_id'] = session('admin_id');
            $update['update_time'] = date('Y-m-d H:i:s');
            $update['is_use'] = $is_use;
            $data = Db::name('commodity_classify')->where('commodity_id',$commodity_id)->update($update);
            if($data!==false){
                if($is_use==1){
                    exit(json_encode(['status'=>1,'msg'=>'上架成功']));
                }else{
                    exit(json_encode(['status'=>1,'msg'=>'下架成功']));
                }
            }else{
                if($is_use==1){
                    exit(json_encode(['status'=>1,'msg'=>'上架失败']));
                }else{
                    exit(json_encode(['status'=>1,'msg'=>'下架失败']));
                }
            }
        }else{
            $this->error('无效的ID', '', '', 1);
        }
    }

}